const http = require('https');
const cheerio = require('cheerio');
const fs = require('fs');

let urlList = [
  { url: 'https://www.aidongwu.net/pet', name: 'pet' },
  { url: 'https://www.aidongwu.net/mammals', name: 'mammals' },
  { url: 'https://www.aidongwu.net/bird', name: 'bird' },
  { url: 'https://www.aidongwu.net/fish', name: 'fish' },
  { url: 'https://www.aidongwu.net/reptilia', name: 'reptilia' },
  { url: 'https://www.aidongwu.net/amphibian', name: 'amphibian' },
  {
    url: 'https://www.aidongwu.net/invertebrate/insect',
    name: 'insect',
  },
  { url: 'https://www.aidongwu.net/invertebrate', name: 'invertebrate' },
];

urlList.forEach(async (item) => {
  // fs.writeFileSync(`./${item.name}.json`, '[]');
  await Pass(item);
});

function GetData(url) {
  return new Promise((r) => {
    http.get(url, function (res) {
      var data = '';
      res.setEncoding('utf8');

      res
        .on('data', function (chunk) {
          data += chunk;
        })
        .on('end', function () {
          r(data);
        });
    });
  });
}

async function Pass({ url, name }) {
  let data = await GetData(url);
  const $ = cheerio.load(data);

  // let navurlList = [];
  //导航栏
  // let navList = [];
  // $('.mainavi > ul > li').each(function (i, elem) {
  //   let navhtml = $(this).html();
  //   let startindex = navhtml.indexOf('<a href="');
  //   let endindex = navhtml.indexOf('>');
  //   let navurl = navhtml.slice(startindex, endindex).split('"')[1];
  //   // navurlList.push({
  //   //   url: navurl,
  //   //   name: navurl.split('/')[3],
  //   // });

  //   navList.push({
  //     id: i + 99,
  //     title: $(this).text(),
  //     path: navurl.split('/')[3],
  //   });
  // });

  // fs.writeFileSync('./nav.json', JSON.stringify(navList));
  // fs.writeFileSync('./url.json', JSON.stringify(navurlList));

  //宠物
  let arr = $('.cat-animal > ul > li');
  let animalList = [];
  arr.each(async function (i, elem) {
    let imghtml = $(this).html();
    let detailurlindex = imghtml.indexOf('<a href="');
    let detailurlendindex = imghtml.indexOf('title="');
    let detailurl = imghtml
      .slice(detailurlindex, detailurlendindex)
      .split('"')[1];

    let startindex = imghtml.indexOf('data-echo="');
    let endindex = imghtml.indexOf('</a>');
    let img = imghtml.slice(startindex, endindex).split('"')[1];
    console.log('imghtml  ----->  ', imghtml);
    await new Promise((resolve) => {
      setTimeout(async () => {
        let detailData = await GetData(detailurl);
        let detail = cheerio.load(detailData)('.tag-animal').text();
        let datas = {
          id: i + 1,
          title: $(this).text(),
          img: img
            ? img
            : 'https://www.aidongwu.net/wp-content/themes/adw2/images/loading.gif',
          detail,
        };
        let list = JSON.parse(fs.readFileSync(`./${name}.json`, 'utf-8'));
        list.push(datas);
        fs.writeFileSync(`./${name}.json`, JSON.stringify(list));
        console.log(`第${i}个已完成  ----->  `);
        resolve(detail);
      }, i * 2000);
    });
  });
}
